package com.yungu.swift.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

/**
 * @Author: cuixiuyin
 * @Date: 2019/11/05
 */

@Slf4j
@Setter
public class OSSTemplate {

    private OSSClient client;

    private String bucketName;

    /**
     * 数据上传
     *
     * @param name 保存的对象名
     * @param data 字节数组
     * @return 上传是否成功
     */
    public boolean upload(String name, byte[] data) {
        try {
            client.putObject(bucketName, name, new ByteArrayInputStream(data));
            return true;
        } catch (Exception e) {
            log.error("[OSSTemplate]Upload Date Error, BucketName: {}, Name: {}, ErrMsg: {}", bucketName, name, e.getMessage());
            return false;
        }
    }

    public boolean upload(String name, byte[] data, String md5) {
        try {
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentMD5(md5);
            client.putObject(bucketName, name, new ByteArrayInputStream(data), meta);
            return true;
        } catch (Exception e) {
            log.error("[OSSTemplate]Upload Date Error, BucketName: {}, Name: {}, ErrMsg: {}", bucketName, name, e.getMessage());
            return false;
        }
    }

    /**
     * 数据上传
     *
     * @param name        保存的对象名
     * @param inputStream 流式输入
     * @return 上传是否成功
     */
    public boolean upload(String name, InputStream inputStream) {
        try {
            client.putObject(bucketName, name, inputStream);
            return true;
        } catch (Exception e) {
            log.error("[OSSTemplate]Upload Date Error, BucketName: {}, Name: {}, ErrMsg: {}", bucketName, name, e.getMessage());
            return false;
        }
    }

    public boolean upload(String name, InputStream inputStream, String md5) {
        try {
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentMD5(md5);
            client.putObject(bucketName, name, inputStream, meta);
            return true;
        } catch (Exception e) {
            log.error("[OSSTemplate]Upload Date Error, BucketName: {}, Name: {}, ErrMsg: {}", bucketName, name, e.getMessage());
            return false;
        }
    }

    /**
     * 数据上传
     *
     * @param name 保存的对象名
     * @param file 对象
     * @return 上传是否成功
     */
    public boolean upload(String name, File file) {
        try {
            client.putObject(bucketName, name, new FileInputStream(file));
            return true;
        } catch (Exception e) {
            log.error("[OSSTemplate]Upload Date Error, BucketName: {}, Name: {}, ErrMsg: {}", bucketName, name, e.getMessage());
            return false;
        }
    }

    public boolean upload(String name, File file, String md5) {
        try {
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentMD5(md5);
            client.putObject(bucketName, name, new FileInputStream(file), meta);
            return true;
        } catch (Exception e) {
            log.error("[OSSTemplate]Upload Date Error, BucketName: {}, Name: {}, ErrMsg: {}", bucketName, name, e.getMessage());
            return false;
        }
    }

    /**
     * 数据下载(数据读取完成后，获取的流必须关闭，否则会造成连接泄漏，导致请求无连接可用，程序无法正常工作)
     *
     * @param name 下载对象名
     * @return 数据输入流
     */
    public InputStream download(String name) {
        try {
            OSSObject object = client.getObject(bucketName, name);
            return object.getObjectContent();
        } catch (Exception e) {
            log.error("[OSSTemplate]Download Date Error, BucketName: {}, Name: {}, ErrMsg: {}", bucketName, name, e.getMessage());
            return null;
        }
    }

    /**
     * 生成的签名URL提供给访客进行临时授权访问
     *
     * @param name       申请临时授权的对象名
     * @param expiration 过期的绝对时间
     * @return 临时授权访问的URL
     */
    public URL generatePresignedUrl(String name, Date expiration) {
        try {
            return client.generatePresignedUrl(bucketName, name, expiration);
        } catch (Exception e) {
            log.error("[OSSTemplate]Download Date Error, BucketName: {}, Name: {}, ErrMsg: {}", bucketName, name, e.getMessage());
            return null;
        }
    }

    /**
     * 数据下载到指定文件
     *
     * @param name 下载对象名
     * @param file
     * @return
     */
    public boolean download(String name, File file) {
        try {
            client.getObject(new GetObjectRequest(bucketName, name), file);
            return true;
        } catch (Exception e) {
            log.error("[OSSTemplate]Download Date Error, BucketName: {}, Name: {}, ErrMsg: {}", bucketName, name, e.getMessage());
            return false;
        }
    }
}
